<template>
  <div class="login">
    <!-- <div>好麦管理后台</div> -->
    <div class="login-main">
    <div class="login-left"/>
    <div class="login-right">
      <div class="login-right-title">Go-Admin后台</div>
      <Row @keydown.enter.native="submitLogin" class="flex">
        <Col style="width: 230px;height: 250px;">
          <Header />
          <Row style="flex-direction: column">
            <Form
              ref="usernameLoginForm"
              :model="form"
              :rules="rules"
              class="form"
            >
              <FormItem prop="username">
                <Input
                  v-model="form.username"
                  prefix="ios-contact"
                  size="large"
                  clearable
                  placeholder="请输入用户名"
                  autocomplete="off"
                />
              </FormItem>
              <FormItem prop="password">
                <Input
                  type="password"
                  v-model="form.password"
                  prefix="ios-lock"
                  size="large"
                  password
                  placeholder="请输入密码"
                  autocomplete="off"
                />
              </FormItem>
            </Form>

            <Row>
              <Button
                class="login-btn"
                type="primary"
                size="large"
                :loading="loading"
                @click="submitLogin"
                long
              >
                <span v-if="!loading">{{ $t("login") }}</span>
                <span v-else>{{ $t("logining") }}</span>
              </Button>
            </Row>
          </Row>
          <!-- 拼图验证码 -->
          <!-- <verify
            ref="verify"
            class="verify-con"
            verifyType="LOGIN"
            @change="verifyChange"
          ></verify> -->
          
        </Col>
        <!-- <LangSwitch /> -->
      </Row>
    </div>
    </div>
    <Footer/>
  </div>
</template>

<script>
import { login, userInfo } from "@/api/index";
import Cookies from "js-cookie";
import Header from "@/views/main-parts/header";
import Footer from "@/views/main-parts/footer";
import LangSwitch from "@/views/main-parts/lang-switch";
import util from "@/libs/util.js";
import {md5} from "@/utils/md5";
// import verify from "@/components/verify";

export default {
  components: {
    LangSwitch,
    Header,
    Footer,
    // verify,
  },
  data() {
    return {
      loading: false, // 加载状态
      form: {
        // 表单数据
        username: "",
        password: "",
        mobile: "",
        code: "",
      },
      rules: {
        // 验证规则
        username: [
          {
            required: true,
            message: "账号不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    afterLogin(res) {
      console.log('afterLogin',res);
      // 登录成功后处理
      let accessToken = res.data.token;
      console.log('accessToken',accessToken);
      this.setStore("accessToken", accessToken);
      // 获取用户信息
      userInfo().then((res) => {
        if (res.code === 200) {
          // 加载菜单
          Cookies.set("userInfoManager", JSON.stringify(res.data));
          this.$store.commit("setAvatarPath", res.data.head_sculpture);
          util.initRouter(this);
          this.$router.push({
            name: "home_index",
          });
        } else {
          this.loading = false;
        }
      });
    },
    submitLogin() {
      // 登录操作
      this.$refs.usernameLoginForm.validate((valid) => {
        if (valid) {
          this.doLogin();
        }
      });
    },
    doLogin() {
      this.loading = true;
      let jsonData = {};
      // jsonData.email = this.form.username;
      // jsonData.password = this.form.password;
      jsonData.account = this.form.username;
      jsonData.password =  md5(this.form.password);
      let jsonString = JSON.stringify(jsonData);
      login(jsonString)
        .then((res) => {
          console.log('res', res, res.code === 200);
          if (res && res.code===200) {
            this.afterLogin(res);
          } else {
            this.loading = false;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

  },
};
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  width: 100%;
  height: 100%;
  background-color: #f5f6fd;
  justify-content: center;
  .login-main {
    display: flex;
    margin: 200px 40vw 0 200px;
    .login-left {
      width: 50vw;
      background: url("../assets/background.png") no-repeat;
      background-size: 60% 60%;
      background-position-x: 20vw;
      align-items: center;
      justify-content: center;
    }

    .login-right {
      display: flex;
      flex-direction: column;
      align-items: center;
      .login-right-title {
        font-size: 30px;
        font-weight: 600;
        text-align: center;
        margin-bottom: 20px;
      }
    }
  

  .verify-con {
    position: absolute;
    top: 150px;
    z-index: 10;
    left: 20px;
  }
  .form {
    padding-top: 1vh;
  }

  .login-btn {
    background: linear-gradient(135deg, $theme_color 0%, $warning_color 100%);
    height: 40px;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #fff;
    width: 100%;
    text-align: center;
    transition: 0.35s;
  }
  .login-btn:hover {
    opacity: 0.9;
    border-radius: 10px;
  }
}
}
.flex {
  padding: 50px;
  border-radius: 20px;
  box-shadow:  0 0 5px rgba(0, 0, 0, 0.3);
  background-color: #ffffff;
}
</style>
